package com.etoppaas.performance.service.impl;

import java.util.List;

import com.etoppaas.performance.domain.PerTemplateSpecRelation;
import com.etoppaas.performance.mapper.PerTemplateSpecRelationMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.etoppaas.performance.mapper.PerTemplateMapper;
import com.etoppaas.performance.domain.PerTemplate;
import com.etoppaas.performance.service.IPerTemplateService;
import com.ruoyi.common.core.text.Convert;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-06-05
 */
@Service("template")
public class PerTemplateServiceImpl implements IPerTemplateService 
{
	@Autowired
	private PerTemplateMapper perTemplateMapper;

	@Autowired
	private PerTemplateSpecRelationMapper perTemplateSpecRelationMapper;

	/**
     * 查询信息
     * 
     * @param templateId ID
     * @return 信息
     */
    @Override
	public PerTemplate selectPerTemplateById(Long templateId)
	{
	    return perTemplateMapper.selectPerTemplateById(templateId);
	}
	
	/**
     * 查询列表
     * 
     * @param perTemplate 信息
     * @return 集合
     */
	@Override
	public List<PerTemplate> selectPerTemplateList(PerTemplate perTemplate)
	{
		perTemplate.setCreateor(ShiroUtils.getUserId());
	    return perTemplateMapper.selectPerTemplateList(perTemplate);
	}
	@Override
	public List<PerTemplate> selectPerTemplateList(){
		PerTemplate perTemplate = new PerTemplate();
		perTemplate.setCreateor(ShiroUtils.getUserId());
		return perTemplateMapper.selectPerTemplateList(perTemplate);
	}
    /**
     * 新增
     * 
     * @param perTemplate 信息
     * @return 结果
     */
	@Override
	public int insertPerTemplate(PerTemplate perTemplate)
	{
		perTemplate.setCreateor(ShiroUtils.getUserId());
	    return perTemplateMapper.insertPerTemplate(perTemplate);
	}
	
	/**
     * 修改
     * 
     * @param perTemplate 信息
     * @return 结果
     */
	@Override
	public int updatePerTemplate(PerTemplate perTemplate)
	{
	    return perTemplateMapper.updatePerTemplate(perTemplate);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deletePerTemplateByIds(String ids)
	{
		return perTemplateMapper.deletePerTemplateByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<PerTemplateSpecRelation> getDistributeData(Long templateId) {
		PerTemplateSpecRelation query = new PerTemplateSpecRelation();
		query.setTemplateId(templateId);
		return perTemplateSpecRelationMapper.selectPerTemplateSpecRelationList(query);
	}

	@Override
	public AjaxResult checkWeightLegal(Long templateId){

		PerTemplateSpecRelation relation = new PerTemplateSpecRelation();
		relation.setTemplateId(templateId);
		List<PerTemplateSpecRelation> relations=perTemplateSpecRelationMapper.selectPerTemplateSpecRelationList(relation);
		if(CollectionUtils.isEmpty(relations)){
			return AjaxResult.error();
		}
		int sumWeight=0;
		for(PerTemplateSpecRelation perTemplateSpecRelation:relations){
			sumWeight+=perTemplateSpecRelation.getWeight();
		}
		if(sumWeight==100){
			return AjaxResult.success();
		}
		return AjaxResult.error();
	}
}
